package com.hancher.gamelife2.colockin;

import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.navigation.Navigation;

import com.hancher.common.androidutil.AssetUtil;
import com.hancher.common.androidutil.AsyncUtils;
import com.hancher.common.androidutil.ToastUtil;
import com.hancher.common.base.BaseFragment;
import com.hancher.common.javautil.EmptyUtil;
import com.hancher.common.rv.PageRecyclerViewManager;
import com.hancher.gamelife2.databinding.FragmentColockInEditBinding;
import com.hancher.gamelife2.main.MainDatabase_Impl;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * 描述 : 添加新的打卡分类 <br/>
 * 时间 : 2024/4/12 15:11 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 */
public class ColockInEditFragment extends BaseFragment<FragmentColockInEditBinding> {


    private PageRecyclerViewManager<String> prvm;
    private ColockInTypePO type;

    @Override
    protected void initView(Bundle savedInstanceState) {
        prvm = new PageRecyclerViewManager<String>()
                .setContext(getContext())
                .setRecyclerView(binding.recyclerview)
                .setAdapter(new ColockInEditAdapter())
                .setDivider(false)
                .setCol(5)
                .setOnItemClickListener((baseQuickAdapter, view, i) -> {
                    Bitmap bitmap = AssetUtil.getImage(getContext(), "colockintype" + File.separator + prvm.getRvList().get(i));
                    binding.imageSelect.setImageBitmap(bitmap);
                    type.setBitmapPath(prvm.getRvList().get(i));
                })
                .setRefreshLayout(binding.refreshLayout)
                .setEnableRefresh(false)
                .setEnableLoadMore(false)
                .init();
        binding.btnFloat.setOnClickListener(v -> {
            // 检查标题是否为空
            if (EmptyUtil.isEmpty(binding.editType.getText().toString())) {
                ToastUtil.show("设置默认打卡标题失败");
                return;
            }
            type.setTitle(binding.editType.getText().toString());
            AsyncUtils.run((ObservableOnSubscribe<String>) emitter -> {
                // TODO: 2024/4/3 保存数据库
                MainDatabase_Impl.getInstance().colockInTypeDao().insertItem(type);
                emitter.onNext("");
            }, s -> Navigation.findNavController(binding.btnFloat).popBackStack());
        });
        type = new ColockInTypePO();
        Bitmap bitmap = AssetUtil.getImage(getContext(), "colockintype" + File.separator + "colock_in.png");
        binding.imageSelect.setImageBitmap(bitmap);
        type.setBitmapPath("colock_in.png");
    }

    @Override
    protected void initData() {
        queryList();
    }

    @Override
    protected void initListener() {
    }

    private void queryList() {
        AsyncUtils.run(emitter -> {
            String[] assetsPath = getContext().getAssets().list("colockintype");
            emitter.onNext(Arrays.asList(assetsPath));
        }, (Consumer<List<String>>) data -> {
            prvm.freshData(data);
        });
    }
}